package com.getsendsocial.GetSendSocial.Database;


import com.getsendsocial.GetSendSocial.Model.FraseModel;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class GSDatabase {  

    SQLiteDatabase mDb;
    GSDbHelper mDbHelper;
    Context mContext;
    private static final String DB_NAME="GSDb";//nome del db
    private static final int DB_VERSION=1; //numero di versione del nostro db
   
    public GSDatabase(Context ctx){
            mContext=ctx;
            mDbHelper=new GSDbHelper(ctx, DB_NAME, null, DB_VERSION);   //quando istanziamo questa classe, istanziamo anche l'helper (vedi sotto)    
    }
   
    public void open(){  //il database su cui agiamo è leggibile/scrivibile
            mDb=mDbHelper.getWritableDatabase();
           
    }
   
    public void close(){ //chiudiamo il database su cui agiamo
            mDb.close();
    }

    
    public String insertFrase(FraseModel[] frases){

    	try{
    		for (FraseModel frase : frases) {
    			if (frase.getType()==0){ //funny
        			ContentValues cv = new ContentValues();
        			cv.put(FunnyTable.FRASE, frase.getId());
        			cv.put(FunnyTable.FRASE, frase.getText());
        			cv.put(FunnyTable.FLAG, 0);
        			mDb.insert(FunnyTable.TABLENAME, null, cv);
        		}else{
        			ContentValues cv = new ContentValues();
        			cv.put(CongrTable.FRASE, frase.getId());
        			cv.put(CongrTable.FRASE, frase.getText());
        			cv.put(CongrTable.FLAG, 0);
        			mDb.insert(CongrTable.TABLENAME, null, cv);
        		}
			}
    		return "SUCCESS";
    	}catch(Exception e){
    		return "DBERROR";
    	}
    }
    
    public String insertFrase(FraseModel frase){

    	try{
    		if (frase.getType()==0){ //funny
        			ContentValues cv = new ContentValues();
        			cv.put(FunnyTable.FRASE, frase.getId());
        			cv.put(FunnyTable.FRASE, frase.getText());
        			cv.put(FunnyTable.FLAG, 0);
        			mDb.insert(FunnyTable.TABLENAME, null, cv);
        		}else{
        			ContentValues cv = new ContentValues();
        			cv.put(CongrTable.FRASE, frase.getId());
        			cv.put(CongrTable.FRASE, frase.getText());
        			cv.put(CongrTable.FLAG, 0);
        			mDb.insert(CongrTable.TABLENAME, null, cv);
        		}
    		return "SUCCESS";
    	}catch(Exception e){
    		return "DBERROR";
    	}
    }
    
    public Cursor toSync(String tableName){
		String getQuery = "SELECT ID FROM '"+ tableName + "' WHERE 1";
		return mDb.rawQuery(getQuery, null);

    }
   
    public Cursor fetchProducts(String tableName){ 
    	
    		String getQuery = "SELECT ID,FRASE FROM '"+ tableName + "' WHERE FLAG=0 ORDER BY Random() LIMIT 0,1";
            return mDb.rawQuery(getQuery, null);            
    }
    
    public void changeFlag(int id,String tableName){
		
		ContentValues cv = new ContentValues();
		cv.put("FLAG",1);
		mDb.update(tableName, cv, "ID"+"="+id, null);
		String verify = "SELECT FLAG FROM " + tableName + " WHERE ID ="+ Integer.toString(id);
		Cursor c = mDb.rawQuery(verify,null);
		while(c.moveToNext()){
			int flag = c.getInt(c.getColumnIndexOrThrow("FLAG"));
			System.out.println(flag);
		}
    }

	public void setFlag(String tableName) {
		
        ContentValues cv = new ContentValues();
		cv.put("FLAG",0);
		mDb.update(tableName, cv, null, null);
	}
}
